﻿<?php

class Model_lesson extends CI_Model {
    
    function __construct() {
        parent::__construct();
    }

    public function getLesson($id) {
        $collection = $this->mongo_db->db->selectCollection('lesson');
        $lesson = $collection->findOne(array('_id'=> $id));      
        return $lesson;
    }

    public function getLessonList() {
         $collection = $this->mongo_db->db->selectCollection('lesson');
        $lessons = $collection->find();      
        return $lessons;
    }

    public function getArrLesson($arr) {
        $collection = $this->mongo_db->db->selectCollection('lesson');
        $lessons = $collection->find(array('_id'=>array('$in'=>$arr)), array('title'=>1));      
        return $lessons;
    }

    public function getCats($type) {
        $collection = $this->mongo_db->db->selectCollection('category');
        $cats = $collection->find(array('type'=> $type));      
        return $cats;
    }
    public function countCatLessons($id) {
        $collection = $this->mongo_db->db->selectCollection('lesson');
        $num = $collection->count(array('cat' => $id));
        return $num;
    }
    public function getLsByCats($id) {
        $collection = $this->mongo_db->db->selectCollection('lesson');
        $lesson = $collection->find(array(
            'cat'=> array('$in' => $id)
        ));
        return $lesson;
    }
    public function getChildCats($id) {
        $collection = $this->mongo_db->db->selectCollection('category');
        $cats = $collection->find(array('parent'=> $id));      
        return $cats;
    }
    public function getLsByEvent($id) {
        $collection = $this->mongo_db->db->selectCollection('lesson');
        $ls = $collection->find(array('events'=> $id), array('title'));      
        return $ls;
    } 


/**
 * ============================SEARCH LESSON FOLLOW TITLE=============================================
*/
     public function searchLesson($text) {
        $collection = $this->mongo_db->db->selectCollection('lesson');
        // $query = $collection->find(array('title'=> array('$regex' => $text)));
        $query = $collection->find(array('title'=> array('$regex' => $text, '$options' => 'i')));
        return $query;
    }


/**
 * ============================UPDATE LESSON=============================================
*/
    public function update_lesson($data, $id)
    {
        $collection = $this->mongo_db->db->selectCollection('lesson');        
        $collection->update(array('_id' => $id), array('$set' => $data));
    }
/**
 * ============================DELETE LESSON =============================================
*/
    public function delete_lesson($id_lesson){
        
        $collection = $this->mongo_db->db->selectCollection('lesson');
        $collection->remove(array('_id' => new MongoId($id_lesson)));
    }

//===============================INSERT NEW LESSON=============================================
    public function insert_lesson($title, $fromy, $toy, $level, $type, $content)
    {
        $collection = $this->mongo_db->db->selectCollection('lesson');
        
        $data = array(
            '_id' =>$id_lesson,
            'title' => $title,
            'fromY' => $fromy,
            'toY' => $toy,
            'level' => $level,
            'type' => $type,
            'content' => $content
        );
        $collection->insert($data);
    }
    public function addLesson($data) {
        $collection = $this->mongo_db->db->selectCollection('lesson');

        $mongoId = new MongoId();
        $mongoId = (string)$mongoId;
        $data['_id'] = $mongoId;

        $collection->insert($data);
    }
}
?>